package com.lebang.service;

import com.alibaba.fastjson.JSON;
import com.lebang.domain.VideoCollectionRecord;
import com.lebang.example.VideoCollectionRecordExample;
import com.lebang.mapper.VideoCollectionRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class VideoCollectionService extends BaseService<VideoCollectionRecord, Integer, VideoCollectionRecordExample> {

    @Autowired
    private VideoCollectionRecordMapper videoCollectionRecordMapper;

    /**
     * 写入用户短视频收藏记录
     *
     * @param videoCollectionRecord 写入记录
     */
    public boolean insertVideoCollection(VideoCollectionRecord videoCollectionRecord) {
        return videoCollectionRecordMapper.insertSelective(videoCollectionRecord) > 0;
    }

    /**
     * json字符串转化为用户收藏记录对象
     *
     * @param param           json对象
     * @param memberId 用户id
     * @return 返回转化结果
     */
    public VideoCollectionRecord parseToObj(String param, String memberId) {
        LocalDateTime now = LocalDateTime.now();
        try {
            VideoCollectionRecord videoCollectionRecord = JSON.parseObject(param, VideoCollectionRecord.class);
            videoCollectionRecord.setCreateTime(now);
            videoCollectionRecord.setHasDeleted(0);
            videoCollectionRecord.setMemberId(memberId);
            videoCollectionRecord.setModifyTime(now);
            return videoCollectionRecord;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 检查是否已经关注短视频作者
     *
     * @return 返回结果
     */
    public boolean checkRecordExit(String memberId,Long mAuthorId) {
        return videoCollectionRecordMapper.checkRecordExit(memberId, mAuthorId) != null;
    }

    /**
     * 查询用户的关注记录
     *
     * @param memberId 用户id
     * @return 关注记录
     */
    public List<VideoCollectionRecord> selectByMemberId(String memberId) {
        return videoCollectionRecordMapper.selectByMemberId(memberId);
    }

    /**
     * 取消收藏
     * @param videoCollectionRecord
     * @return
     */
    public boolean cancelVideoCollection(VideoCollectionRecord videoCollectionRecord) {
        return videoCollectionRecordMapper.deleteByMemberAndAuthor(videoCollectionRecord.getMemberId(),videoCollectionRecord.getmAuthorId())>0;
    }

    /**
     * 查找用户关注记录
     * @return
     */
    public VideoCollectionRecord selectCollectRecordByMemberIdAndAuthorId(String memberId,Long authorId) {
        return videoCollectionRecordMapper.selectCollectRecordByMemberIdAndAuthorId(memberId,authorId);
    }
}
